package com.example.util;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Objects;

/**
 * @Description
 * @author: Zhou
 * @Date: 2021/2/22
 */
public class ReadFileUtil {

    /**
     * 读取json文件
     */
    public static String readJsonFile(String fileName) {
        String jsonStr;
        try {
            File jsonFile = new File(fileName);
            FileReader fileReader = new FileReader(jsonFile);
            Reader reader = new InputStreamReader(new FileInputStream(jsonFile),StandardCharsets.UTF_8);
            int ch;
            StringBuilder sb = new StringBuilder();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            fileReader.close();
            reader.close();
            jsonStr = sb.toString();
            return jsonStr;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) {
        String path = Objects.requireNonNull(ReadFileUtil.class.getClassLoader().getResource("templates/package.json")).getPath();
        System.out.println(path);
        String s = ReadFileUtil.readJsonFile(path);
        System.err.println(s);
    }
}
